home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / os20 / cli / mkdir13.lha / mkdir / mkdir.c < prev    next >
C/C++ Source or Header  |  1992-11-01  |  6KB  |  281 lines

  1.  
  2. /*
  3.  *  mkdir.c
  4.  *
  5.  *  Author: Georg Hessmann (hessmann@fmi.uni-passau-de)
  6.  *
  7.  *  Copyright: source is public domain, no copyright
  8.  *
  9.  *  Version history:
  10.  *
  11.  *  1.0  25.Sep.92  First release.
  12.  *
  13.  *  1.1  04.Okt.92  Add DEFAULT_ICON define.
  14.  *                  If the define is defined, than mkdir creates always
  15.  *                  a icon, exept NOICON is given.
  16.  *                  If DEFAULT_ICON is not defined, mkdir works like the 
  17.  *                  c:makedir, exept the keyword ICON is given.
  18.  *
  19.  *  1.2  27.Okt.92  Add SAS/C 6.0 support. Initial function needs __saveds!
  20.  *
  21.  *  1.3  01.Nov.92  Borrowed a idea of James McDonald's and Mark McPherson's
  22.  *                  mmdir. Now mkdir can create multiple directories at once.
  23.  *                  (e.g. ram:not_exist_1/not_exist_2 will create both dirs)
  24.  *                  mkdir is a good example of the power of C:
  25.  *                  mkdir does more than mmdir, but is smaller than mmdir.
  26.  *                  But mmdir is completly in ASM, mkdir in C.
  27.  *
  28.  */
  29.  
  30.  
  31. #define VERSION "1.3"
  32.  
  33. static const char version[] = "\0$VER: mkdir " VERSION " (11/01/92)";
  34.  
  35.  
  36. #define SysBase        pb->pb_SysBase
  37. #define DOSBase        pb->pb_DOSBase
  38. #define IconBase    pb->pb_IconBase
  39.  
  40.  
  41. /*
  42.  * Amiga-Includes
  43.  */
  44.  
  45. #include <exec/types.h>
  46. #include <dos/dos.h>
  47. #include <dos/dosasl.h>
  48. #include <workbench/workbench.h>
  49.  
  50. #include <clib/dos_protos.h>
  51. #include <clib/exec_protos.h>
  52. #include <clib/icon_protos.h>
  53. #include <pragmas/dos_pragmas.h>
  54. #include <pragmas/exec_pragmas.h>
  55. #include <pragmas/icon_pragmas.h>
  56.  
  57.  
  58. /*
  59.  * Define SAS/C builtin function
  60.  */
  61.  
  62. #define strlen __builtin_strlen
  63. extern int strlen(char *);
  64.  
  65.  
  66. /*
  67.  * Usefull macro to check Ctrl-C
  68.  */
  69.  
  70. #define IsCtrlC        (SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  71.  
  72.  
  73.  
  74. /*
  75.  * Used error strings
  76.  */
  77.  
  78. #define MSG_DI_OBJ    "Can't create icon %s.info\n"
  79. #define MSG_ERR_CRE    "Can't create directory %s\n"
  80. #define MSG_ALR_EX    "%s already exists\n"
  81. #define MSG_NO_ARG    "No name given\n"
  82.  
  83.  
  84. /*
  85.  * Template for ReadArgs()
  86.  */
  87.  
  88. #if defined(DEFAULT_ICON)
  89. #  define TEMPLATE    "NI=NOICON/S,NAME/M"
  90. #else
  91. #  define TEMPLATE    "ICON/S,NAME/M"
  92. #endif
  93.  
  94.  
  95.  
  96. /*
  97.  * Structure of pseudo global variables.
  98.  */
  99.  
  100. struct ParamBlock {
  101. #if defined(__USE_SYSBASE) || !defined(__SASC_60)
  102.   struct Library       * pb_SysBase;
  103. #endif
  104.   struct Library       * pb_DOSBase;
  105.   struct Library       * pb_SysBase;
  106.   struct Library       * pb_IconBase;
  107.   int                    pb_CreateIcons;
  108. };
  109.  
  110.  
  111. /*
  112.  * Prototypes of help functions
  113.  */
  114.  
  115. static int CreateTree(struct ParamBlock * pb, char * ptr);
  116. static int CreateOneDir(struct ParamBlock * pb, char * ptr);
  117.  
  118.  
  119. /*
  120.  * Main function. Must be the first in the module
  121.  */
  122.  
  123. long __saveds main(void)
  124. {
  125.   long rc = RETURN_OK;
  126.   long options[2];
  127.   struct RDArgs * args;
  128.   struct ParamBlock Params;
  129.   struct ParamBlock * pb = &Params;
  130.  
  131. #if defined(__USE_SYSBASE) || !defined(__SASC_60)
  132.   Params.pb_SysBase  = *(struct Library **)(4);
  133. #endif
  134.   Params.pb_DOSBase  = OpenLibrary("dos.library", 37);
  135.   Params.pb_IconBase = OpenLibrary("icon.library", 37);
  136.   if (!(Params.pb_DOSBase && Params.pb_IconBase)) return RETURN_FAIL;
  137.  
  138.   options[0] = options[1] = 0L;
  139.   args = ReadArgs(TEMPLATE, options, NULL);
  140.   if (args) {
  141.     char ** dirs = (char **)options[1];
  142.     if (dirs) {
  143.       char * ptr;
  144.  
  145. #if defined(DEFAULT_ICON)
  146.       Params.pb_CreateIcons = !options[0];    // key -- NOICON
  147. #else
  148.       Params.pb_CreateIcons = options[0];    // key -- ICON
  149. #endif
  150.  
  151.       for (; rc == RETURN_OK && (ptr = *dirs); dirs++) {
  152.         BPTR lock;
  153.  
  154.         lock = Lock(ptr, ACCESS_READ);
  155.         if (lock) {
  156.           UnLock(lock);
  157.           VPrintf(MSG_ALR_EX, (long*)&ptr);
  158.           rc = RETURN_ERROR;
  159.         }
  160.         else {
  161.           rc = CreateTree(pb, ptr);
  162.         }
  163.       }
  164.     
  165.       FreeArgs(args);
  166.     }
  167.     else {
  168.       PutStr(MSG_NO_ARG);
  169.       rc = RETURN_ERROR;
  170.     }
  171.   }
  172.   else {
  173.     PrintFault(IoErr(), NULL);
  174.     rc = RETURN_ERROR;
  175.   }
  176.   
  177.   CloseLibrary(Params.pb_IconBase);
  178.   CloseLibrary(Params.pb_DOSBase);
  179.  
  180.   return rc;
  181. }
  182.  
  183.  
  184. /*
  185.  *  CreateTree()
  186.  *
  187.  *  Create all not existing directories of path 'ptr'.
  188.  *  Find recursive the first existing dir of the path.
  189.  *  Than with the solving of the recursion, create all
  190.  *  needed directories.
  191.  *  Check for Ctrl-C, too.
  192.  *
  193.  */
  194.  
  195. static int CreateTree(struct ParamBlock * pb, char * ptr)
  196. {
  197.   BPTR lock;
  198.   int rc;
  199.   char * t;
  200.  
  201.   /* first: search a existing super-dir */
  202.   for (t=ptr+strlen(ptr)-1;
  203.        t >= ptr && *t != '/' && *t != ':';
  204.        t--) ;
  205.  
  206.   if (IsCtrlC) {
  207.     PrintFault(ERROR_BREAK, NULL);
  208.     rc = RETURN_WARN;
  209.   }
  210.   else {
  211.  
  212.     if (*t == '/') {
  213.       *t = '\0';                // nun ist der Pfad etwas kuerzer
  214.       lock = Lock(ptr, ACCESS_READ);
  215.       if (lock) {
  216.         // das uebergeordnete Dir. existiert, also erzeuge dieses Dir. nun
  217.         UnLock(lock);
  218.         *t = '/';                // urspruenglichen Zustand wieder herstellen
  219.         // und nun Directory erzeugen (wurde nach hinten verlegt)
  220.       }
  221.       else {
  222.         // dies ist immer noch nicht das Ueber-Directory, also weitersuchen
  223.         rc = CreateTree(pb, ptr);
  224.         *t = '/';                // urspruenglichen Zustand wieder herstellen
  225.         // und nun (falls rc == RETURN_OK) Directory erzeugen (wurde nach hinten verlegt)
  226.       }
  227.     }
  228.     else {
  229.       // kein '/' gefunden (':' oder gar nichts)
  230.       // dann erzeuge einfach den kompletten String als Dir.
  231.       // und nun Directory erzeugen (wurde nach hinten verlegt)
  232.     }
  233.  
  234.   }
  235.  
  236.   if (rc == RETURN_OK) {  
  237.     rc = CreateOneDir(pb, ptr);
  238.   }
  239.  
  240.   return rc;
  241. }
  242.  
  243.  
  244. /*
  245.  *  CreateOneDir()
  246.  *
  247.  *  Create directory 'ptr' and give it a .info file, if 
  248.  *  pb->pb_CreateIcons is set.
  249.  *
  250.  */
  251.  
  252. static int CreateOneDir(struct ParamBlock * pb, char * ptr)
  253. {
  254.   BPTR lock;
  255.   int rc = RETURN_OK;
  256.  
  257.   lock = CreateDir(ptr);
  258.  
  259.   if (lock) {
  260.     if (pb->pb_CreateIcons) {
  261.       struct DiskObject * di = GetDefDiskObject(WBDRAWER);
  262.       if (di && PutDiskObject(ptr, di)) {
  263.         FreeDiskObject(di);
  264.       }
  265.       else {
  266.         VPrintf(MSG_DI_OBJ, (long *)&ptr);
  267.         PrintFault(IoErr(), NULL);
  268.         rc = RETURN_ERROR;
  269.       }
  270.     }
  271.     UnLock(lock);
  272.   }
  273.   else {
  274.     VPrintf(MSG_ERR_CRE, (long*)&ptr);
  275.     PrintFault(IoErr(), NULL);
  276.     rc = RETURN_ERROR;
  277.   }
  278.  
  279.   return rc;
  280. }
  281.